package com.blue.utils;

import javafx.concurrent.Task;

import java.applet.Applet;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2019/7/3.
 */
public class Firing extends Applet {
    private static final String ip = "127.0.0.1";
    private static final int nport = 8009;
    private static Boolean flag = false;
    public static Object ob = "safe";

    public Firing() throws HeadlessException {
    }

    public Task<Boolean> excutetomcat(String file, Boolean isCloseWindow) {

        return new Task<Boolean>() {
            @Override
            protected Boolean call() throws Exception {
                String cmdCommand = null;
                if (isCloseWindow) {
                    cmdCommand = "cmd /C start /b " + file;
                } else {
                    cmdCommand = "cmd.exe /k start " + file;
                }
                StringBuilder stringBuilder = new StringBuilder();
                Process process = null;
                try {
                    System.out.println( cmdCommand );
                    process = Runtime.getRuntime().exec( cmdCommand );
                    if (process != null) {
                        BufferedReader bufferedReader = new BufferedReader(
                                new InputStreamReader( process.getInputStream(), "utf-8" ) );
                        String line = null;
                        while ((line = bufferedReader.readLine()) != null) {
                            stringBuilder.append( line + "\n" );
                            System.out.println( "输出结果为：" + stringBuilder );
                        }
                    }
                    updateMessage( "the Tomcat is in the process of being started,please wait..." );
                    Thread.sleep( 1500 );
                    Boolean heartbeat = BatUtils.heartbeat();
                    System.out.println( "first task heartbeat*******" + heartbeat );
                    return heartbeat;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return false;
            }

            @Override
            protected void succeeded() {
                super.succeeded();
                updateMessage( "Tomcat being started successfully!" );
                try {
                    Thread.sleep( 1500 );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public Task excutetomcatlinux(String command) {

        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                updateMessage( "the Tomcat is in the process of being started,please wait..." );
                StringBuilder stringBuilder = new StringBuilder();
                InputStream in = null;
                String line = null;
                try {
                    Process pro = Runtime.getRuntime().exec( command );
                    pro.waitFor();
                    in = pro.getInputStream();
                    BufferedReader read = new BufferedReader( new InputStreamReader( in ) );
                    while ((line = read.readLine()) != null) {
                        stringBuilder.append( line + "\n" );
                        System.out.println( "输出结果为：" + stringBuilder );
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void succeeded() {
                super.succeeded();
                updateMessage( "success" );
            }

        };
    }

    public Task excutetomcatlinux2(String[] command) {

        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                StringBuilder stringBuilder = new StringBuilder();
                InputStream in = null;
                String line = null;
                try {
                    Process pro = Runtime.getRuntime().exec( command );
                    pro.waitFor();
                    pro.destroy();
                    //用于处理Runtime.getRuntime().exec产生的错误流及输出流 ,防止阻塞
                    StreamGobbler errorGobbler = new StreamGobbler( pro.getErrorStream(), "ERROR" );
                    errorGobbler.start();
                    StreamGobbler outGobbler = new StreamGobbler( pro.getInputStream(), "STDOUT" );
                    outGobbler.start();

                    in = pro.getInputStream();
                    BufferedReader read = new BufferedReader( new InputStreamReader( in ) );
                    while ((line = read.readLine()) != null) {
                        stringBuilder.append( line + "\n" );
                        System.out.println( "输出结果为：" + stringBuilder );
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

        };
    }

    public void executeLinux2(String[] command) {
        StringBuilder stringBuilder = new StringBuilder();
        InputStream in = null;
        String line = null;
        try {
            Process pro = Runtime.getRuntime().exec( command );
            pro.waitFor();
            in = pro.getInputStream();
            BufferedReader read = new BufferedReader( new InputStreamReader( in ) );
            while ((line = read.readLine()) != null) {
                stringBuilder.append( line + "\n" );
                System.out.println( "输出结果为：" + stringBuilder );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
